<?php

/**
 * Created by PhpStorm.
 * User: dell
 * Date: 2019/8/21
 * Time: 15:37
 * 汉语平台，用户管理
 * api封装接口
 *
 *
 */

namespace app\index\controller;

use think\Controller;
use think\Db;
use think\Request;
use think\Session;

header('Access-Control-Allow-Origin:*');
header('Access-Control-Allow-Methods:POST,GET,OPTIONS');
header('Access-Control-Allow-Headers:x-requested-with,content-type,requesttype,token');

class Api extends Controller
{
    public function apkList()
    {
        $rtype = 1;
        $result = Db::table('apk')->order('id desc')->select();
        $this->return_val($result, $rtype);
    }

    public function apkList1()
    {
        $id = $_GET['id'];
        $rtype = 1;
        $result = Db::table('apk')->where('id', $id)->select();
        $this->return_val($result, $rtype);

    }

    public function add()
    {
        $name = $_POST["name"];
        $ver = $_POST["ver"];
        $time = time();
        $desc = $_POST['desc'];
        $url = $_POST['url'];
        $rtype = 2;
        $data = ['name' => $name, 'ver' => $ver, 'desc' => $desc, 'url' => $url, 'time' => $time];
        $result = Db::table('apk')->readMaster()->insert($data);
        return $this->return_val($result, $rtype);
    }

    public function ceshi()
    {
        $num=$_POST['action'];
        $dat = array(
            'code' => 200,
            'msg' => "success",
            'res' => $num
        );
        echo json_encode($dat);
    }

    public function visitor()
    {
        $nickName = $_POST['nickName'];
        $touxiang = $_POST['touxiang'];
        $gender = $_POST['gender'];
        $province = $_POST['province'];
        $ip = $_SERVER["REMOTE_ADDR"];
        $d_time = time();
        $data = ['nickName' => $nickName, 'touxiang' => $touxiang, 'gender' => $gender, 'province' => $province, 'ip' => $ip, 'a_time' => $d_time];
        $result = Db::table('visitor')->readMaster()->insert($data);
        $rtype = 2;
        return $this->return_val($result, $rtype);
    }

    /**
     * 通用返回数据格式
     */
    public function returnMsg($res, $msg, $code)
    {
        $dat = array(
            'code' => $code,
            'msg' => $msg,
            'res' => $res
        );
        echo json_encode($dat);
    }

    /**
     * 返回的数据通用格式
     */
    public function return_val($data, $rtype)
    {

        switch ($rtype) {
            case 1:
                $msg = "查询";
                break;
            case 2:
                $msg = "添加";
                break;
            case 3:
                $msg = "该账号已存在，添加";
                break;
            case 4:
                $msg = "登录";
                break;
            case 5:
                $msg = "注册";
                break;
            case 6:
                $msg = "修改";
                break;
        }
        if ($data) {
            $code = 0;
            $msg = $msg . "成功";
        } else {
            $code = 404;
            $msg = $msg . "失败";
        }
        $dat = array(
            'code' => $code,
            'msg' => $msg,
            'data' => $data
        );
        echo json_encode($dat);
    }


}
